package com.xudow.app.dynamicstate_old.module.discovery.search;

import com.jude.beam.expansion.list.BeamListFragmentPresenter;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.server.ErrorTransform;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;

/**
 * Created by zhuchenxi on 16/6/24.
 */

public class DynamicSearchPresenter extends BeamListFragmentPresenter<DynamicSearchFragment, Dynamic> {
    long lastTime = 0;
    String word;


    @Override
    public void onRefresh() {
        lastTime = 0;
        DynamicModel.getInstance()
                .searchDynamic(word, lastTime, 1, 10)
                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                .doOnNext(list -> {
                    if (list != null && list.size() > 0)
                        lastTime = list.get(list.size() - 1).getPublishTime();
                })
                .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                .unsafeSubscribe(getRefreshSubscriber());
    }

    @Override
    public void onLoadMore() {
        DynamicModel.getInstance()
                .searchDynamic(word, lastTime, 2, 10)
                .compose(new ErrorTransform<>(ErrorTransform.ServerErrorHandler.TOAST))
                .doOnNext(list -> {
                    if (list != null && list.size() > 0)
                        lastTime = list.get(list.size() - 1).getPublishTime();
                })
//                .doAfterTerminate(() -> getAdapter().notifyDataSetChanged())
                .unsafeSubscribe(getMoreSubscriber());
    }
}
